<?php

/**
 * @author dungdhs@gmail.com
 * @copyright 2012
 */


class SimpleImage {
 
  var $image;
  var $image_type;
 
  function load($filename) {
 
    $image_info = getimagesize($filename);
    $this->image_type = $image_info[2];
    if( $this->image_type == IMAGETYPE_JPEG ) {
 
      $this->image = imagecreatefromjpeg($filename);
    } elseif( $this->image_type == IMAGETYPE_GIF ) {
 
      $this->image = imagecreatefromgif($filename);
    } elseif( $this->image_type == IMAGETYPE_PNG ) {
 
      $this->image = imagecreatefrompng($filename);
    }
  }
  function save($filename, $image_type=IMAGETYPE_JPEG, $compression=100, $permissions=null) {
 
    if( $image_type == IMAGETYPE_JPEG ) {
      header('Content-Type: image/jpeg');
      imagejpeg($this->image,$filename,$compression);
    } elseif( $image_type == IMAGETYPE_GIF ) {
      header('Content-Type: image/gif');
      imagegif($this->image,$filename);
    } elseif( $image_type == IMAGETYPE_PNG ) {
      header('Content-Type: image/png');
      imagepng($this->image,$filename);
    }
    if( $permissions != null) { 
      chmod($filename,$permissions);
    }
  }
  function output($image_type=null) {
    if (is_null($image_type)) $image_type = $this->image_type;
    
    if( $image_type == IMAGETYPE_JPEG ) {
      imagejpeg($this->image);
    } elseif( $image_type == IMAGETYPE_GIF ) {
 
      imagegif($this->image);
    } elseif( $image_type == IMAGETYPE_PNG ) {
 
      imagepng($this->image);
    }
  }
  function getWidth() {
 
    return imagesx($this->image);
  }
  function getHeight() {
 
    return imagesy($this->image);
  }
    
  function cropTo($width, $height) { 
    $scale = $this->getWidth()/$this->getHeight();
    $scaleTo = $width/$height;
    
    if ($scale<$scaleTo){
      $this->resizeToWidth($width);
    }else{      
      $this->resizeToHeight($height);
    }
    
    $new_image = imagecreatetruecolor($width, $height);
    imagecopyresampled($new_image, $this->image, 0, 0, floor(($this->getWidth()-$width)/2), floor(($this->getHeight()-$height)/2), $width, $height, $width, $height);
    $this->image = $new_image;
  }
  
  function resizeToHeight($height) {
 
    $ratio = $height / $this->getHeight();
    $width = $this->getWidth() * $ratio;
    $this->resize($width,$height);
  }
 
  function resizeToWidth($width) {
    $ratio = $width / $this->getWidth();
    $height = $this->getheight() * $ratio;
    $this->resize($width,$height);
  }
 
  function scale($scale) {
    $width = $this->getWidth() * $scale/100;
    $height = $this->getheight() * $scale/100;
    $this->resize($width,$height);
  }  
 
  function scaleTo($width, $height) {
    $scale = $this->getWidth()/$this->getHeight();
    $scaleTo = $width/$height;
    
    if ($scale<$scaleTo){
      $width = $height*$scale;
    }else{
      $height = $width/$scale;
    }
    
    $this->resize($width,$height);
  }
 
  function resize($width,$height) {
    $new_image = imagecreatetruecolor($width, $height);
    imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
    $this->image = $new_image;
  } 
}

function files_task(){
  return array(
   'thumb' => array(
    'func'  => '_files_thumb',
    'perm'  => 'file view thumb'
   )
  );
}

function _files_thumb(){
  $args = arg();
  $args = array_slice($args, 2);
  $thumb = array_shift($args);
  $file_path = implode('/', $args);
  
  if (!file_exists(PG_ROOT.'files/'.$file_path)) system_notfound();
  
  _files_create_thumb($thumb, $file_path);
  echo PG_ROOT.'files/'.$file_path; exit();
}

function _files_create_thumb($thumb, $file_path){
  $aFileThumb = image_thumb_settings();
    
  if (!isset($aFileThumb[$thumb])) system_notfound();
  
  $thumbSetting = $aFileThumb[$thumb];
  
  $image = new SimpleImage();
  $image->load(PG_ROOT.'files/'.$file_path);
  
  switch ($thumbSetting['type']){
   case 'scale':
    if ($thumbSetting['width']==0) $image->resizeToHeight($thumbSetting['height']);
    elseif ($thumbSetting['height']==0)  $image->resizeToWidth($thumbSetting['width']);
    else $image->scaleTo($thumbSetting['width'], $thumbSetting['height']); 
    break;
   case 'crop':
    $image->cropTo($thumbSetting['width'], $thumbSetting['height']);
    break;
   case 'resizecrop':
    $image->scaleTo($thumbSetting['width'], $thumbSetting['height']);
    $image->cropTo($thumbSetting['width'], $thumbSetting['height']);
    break;
  }
  
  $aDir = explode('/', 'files/thumb/'.$thumb.'/'.$file_path);
  array_pop($aDir); file_check_dir(implode('/', $aDir));
  
  $image->save(PG_ROOT.'files/thumb/'.$thumb.'/'.$file_path);
  $image->output();
}

?>